;
(function() {
    var role = require("./role");
    var ResponObj = require("../_include/responObj");
    var BusCon = require("../../core/bus/_con");
    module.exports = {
        prefix: "/admin/bus",
        get: {
            "/list": function() {
                var admin = role.getAdmin(this);
                var query = this.request.query;
                var result = admin.getBusList(query.num, query.page);
                console.log("/admin/bus/list", result.length)
                this.body = ResponObj("json", result);
            }
        },
        put: {
            "/secondary_domain": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                var new_second_domain = body.new_second_domain;
                var bus = BusCon.get(bus_id);
                if (!bus) {
                    throwCusError("找不到指定商家")
                }
                bus._updateSecondaryDomain(new_second_domain);
            },
            "/state": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                var state = body.state;
                if (!(bus_id && state)) {
                    throwCusError("参数错误")
                }
                var bus = admin.updateBusState(bus_id, state);
                this.body = ResponObj("json", bus);
            },
            "/permission/:permission_key": function() {
                var admin = role.getAdmin(this);
                var permission_key = this.params.permission_key;
                var body = this.request.body;
                var bus_id = body.bus_id;
                if (!bus_id) {
                    throwCusError("参数错误")
                }
                var bus = admin.updateBusPermission(bus_id, permission_key, body.value);
                this.body = ResponObj("json", bus);
            },
            "/expiration_time": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                var expiration_time = body.expiration_time;
                if (!(bus_id && expiration_time)) {
                    throwCusError("参数错误")
                }
                var bus = admin.updateBusExpirationTime(bus_id, expiration_time);
                this.body = ResponObj("json", bus);
            },
            "/mobile_theme_name": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                var mobile_theme_name = body.mobile_theme_name;
                if (!(bus_id && mobile_theme_name)) {
                    throwCusError("参数错误")
                }
                var bus = admin.setBusMobileTheme(bus_id, mobile_theme_name);
                this.body = ResponObj("json", bus);
            },
            "/nav_perms": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                var perms_json = body.perms_json;
                if (!(bus_id && perms_json)) {
                    throwCusError("参数错误");
                }
                try {
                    var perms = JSON.parse(perms_json);
                } catch (e) {
                    throwCusError("数据格式有误，无法正确解析")
                }
                var bus = admin.setBusNavPerms(bus_id, perms);
                this.body = ResponObj("json", bus);
            },
            // "/management_category":function () {
            // 	var admin = role.getAdmin(this);
            // 	var body = this.request.body;
            // 	var bus_id = body.bus_id;
            // 	var mobile_theme_name = body.mobile_theme_name;
            // 	if (!(bus_id && mobile_theme_name)) {
            // 		throw "参数错误"
            // 	}
            // 	var bus = admin.setBusMobileTheme(bus_id, mobile_theme_name);
            // 	this.body = ResponObj("json", bus);
            // }
        },
        "delete": {
            "/remove_bus": function() {
                var admin = role.getAdmin(this);
                var body = this.request.body;
                var bus_id = body.bus_id;
                admin.removeBus(bus_id);
                module.exports.get["/list"].apply(this, arguments);
            },
            "/reset_bus/:bus_id": function() {
                var admin = role.getAdmin(this);
                var bus_id = this.params.bus_id;
                this.body = ResponObj("json", admin.clearBusData(bus_id));
            },
            "/clear_bus_goods/:bus_id": function() {
                var admin = role.getAdmin(this);
                var bus_id = this.params.bus_id;
                this.body = ResponObj("json", admin.clearBusGoods(bus_id));
            },
            "/clear_bus_ach/:bus_id": function() {
                var admin = role.getAdmin(this);
                var bus_id = this.params.bus_id;
                this.body = ResponObj("json", admin.clearBusAch(bus_id));
            }
        }
    }
}());
